<template>
	<view :style="{
        backgroundColor: backgroundColor
    }">
		<block v-if="onlyshow">
			<view class="workspace onlyshow">
				<view v-for="(item,index) in data" :key="item.symbol" class="item-box">
					<!-- 文本 -->
					<view v-if="item.type == 'text'" class="text">{{item.content}}</view>
					<!-- 图片 -->
					<view v-if="item.type == 'image'">
						<image class="image" mode="widthFix" :src="item.content" style="width: 100%;"></image>
					</view>
					<!-- 视频 -->
					<view v-if="item.type == 'video'">
						<video :src="item.content" style="width: 100%;"></video>
					</view>
				</view>
			</view>
		</block>
		<block v-else>
			<view class="workspace">
				<view v-for="(item,index) in data" :key="item.id" class="item-box">
					<!-- 文本 -->
					<view v-if="item.type == 'text'">
						<textarea auto-height placeholder="请输入内容" class="textarea" v-model="item.content"
							:maxlength="-1"></textarea>
					</view>
					<!-- 图片 -->
					<view v-if="item.type == 'image'">
						<image class="image" mode="widthFix" :src="item.content" style="width: 100%;"></image>
					</view>
					<!-- 视频 -->
					<view v-if="item.type == 'video'">
						<video :src="item.content" style="width: 100%;"></video>
					</view>
					<!-- 操作 -->
					<view class="operation">
						<view class="btn btn-up" v-if="index > 0" @click="up(index, item)">
							<!-- <image class="image" src="../../static/zhangyuhao-simple-editor/up.png"></image> -->
							<text class="iconfont icon-arrowtriangularup text-red"></text>
						</view>
						<view class="btn btn-down" v-if="index < data.length-1" @click="down(index, item)">
							<!-- <image class="image" src="../../static/zhangyuhao-simple-editor/down.png"></image> -->
							<text class="iconfont icon-arrowtriangulardown text-red"></text>
						</view>
						<view class="btn btn-del" @click="del(index, item)">
							<!-- <image class="image" src="../../static/zhangyuhao-simple-editor/del.png"></image> -->
							<text class="iconfont icon-top text-red"></text>
						</view>
					</view>
				</view>
			</view>
			<view class="btn-box">
				<block v-for="(btn,index) in btns" :key="index">
					<view class="btn" :key="btn" v-if="btn == 'text'" hover-class="btn-hover" hover-stay-time="150"
						@click="add('text')">
						<!-- <image class="image" src="../../static/zhangyuhao-simple-editor/text.png"></image> -->
						
						<text class="cuIcon-text "></text>
					</view>
					<view class="btn" :key="btn" v-if="btn == 'image'" hover-class="btn-hover" hover-stay-time="150"
						@click="chooseImage()">
						<!-- <image class="image" src="../../static/zhangyuhao-simple-editor/image.png"></image> -->
						<text class="cuIcon-camerafill"></text>
					</view>
					<view class="btn" :key="btn" v-if="btn == 'video'" hover-class="btn-hover" hover-stay-time="150"
						@click="chooseVideo()">
						<!-- <image class="image" src="../../static/zhangyuhao-simple-editor/video.png"></image> -->
						<text class="cuIcon-videofill"></text>
					</view>
				</block>
			</view>
			<view class="tips" @click="test()">请添加您的内容</view>
		</block>
	</view>
</template>

<script>
	import HTMLParser from "./html-parser.js";
	import upload from "@/wx-cos/upload.js";
	export default {
		name: "zhangyuhao-simple-editor",
		props: {
			// 初始的html
			value: {
				type: String,
				default: ''
			},
			onlyshow: {
				type: Boolean,
				default: false,
			},
			// 上传的地址
			action: {
				type: String,
				default: ''
			},
			// 上传文件的文件名
			fileName: {
				type: String,
				default: 'file'
			},
			// 额外参数
			formData: {
				type: Object,
				default () {
					return {}
				}
			},
			header: {
				type: Object,
				default () {
					return {}
				}
			},
			// 组件背景颜色
			backgroundColor: {
				default: '#FFF'
			},
			// 可以编辑的按钮
			btns: {
				type: Array,
				default () {
					return ['text', 'image', 'video'];
				},
				validator: function(value) {
					const set = new Set(value);
					// 不可包含重复项
					return set.size == value.length;
				}
			},
			// 上传成功的处理函数，要求返回格式为{success:true,url:'xxx'}的对象
			uploadSuccess: {
				// type: Function,
				default: (res) => {
					return {
						success: false,
						url: ''
					}
				}
			},
			// 图片大小限制
			imageMaxSize: {
				type: Number,
				default: 0
			},
			// 视频大小限制
			videoMaxSize: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				index: 0,
				data: [] // 供操作数据
			};
		},
		created() {

		},
		mounted() {
			// 初始化内容
			const html = new HTMLParser(this.value);
			var data = [];
			for (var i = 0; i < html.length; i++) {
				var type = '';
				var content = '';
				if (html[i].name == 'div') {
					type = "text";
					content = html[i].children ? html[i].children[0].text : '';
				} else if (html[i].name == 'img') {
					type = "image";
					content = html[i].attrs.src;
				} else if (html[i].name == 'video') {
					type = "video";
					content = html[i].attrs.src;
				}
				data.push({
					type: type,
					content: content || '',
					id: type + i
				});
			}
			this.data = data; // 存到data里，供操作
			this.index = data.length; // 初始化最大index
			console.log(this.data)
		},
		computed: {
			html() {
				let result = []
				this.data.forEach(item => {
					if (item.type == 'text') {
						result.push(`<div>${item.content}</div>`)
					} else if (item.type == 'image') {
						result.push(`<img src="${item.content}" style="width: 100%;" />`)
					} else if (item.type == 'video') {
						result.push(`<video src="${item.content}" style="width: 100%;"></video>`)
					}
				})
				return result.join('');
			}
		},
		watch: {
			html(val) {
				this.$emit('input', val)
			}
		},
		methods: {
			test() {
				console.log(this.html)
			},
			// 添加元素
			add(type, content) {
				this.data.push({
					type: type,
					content: content || '',
					id: type + this.index++
				});
				console.log(this.data)
			},
			del(index) {
				uni.showModal({
					title: '提示',
					content: '确定要删除该区块吗？',
					success: (res) => {
						if (res.confirm) {
							this.data.splice(index, 1);
						}
					}
				});
			},
			up(index) {
				let ele = this.data.splice(index, 1)[0];
				this.data.splice(index - 1, 0, ele)
			},
			down(index) {
				let ele = this.data.splice(index, 1)[0];
				this.data.splice(index + 1, 0, ele)
			},
			// 选择图片并上传
			chooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					// sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imageMaxSize && res.tempFiles[0].size > this.imageMaxSize * 1024 * 1024) {
							uni.showModal({
								title: '提示',
								content: `文件过大，请上传小于${this.imageMaxSize}MB的文件`,
								showCancel: false
							})
							return;
						}
						uni.showLoading({
							title: '上传中',
							mask: true
						});
						
						
						upload.uploadFile(this.fileName, res.tempFilePaths[0]).then(data  => {
							let url = "https://" + data.Location;
							this.add('image', url);
							let result = this.uploadSuccess(data);
						});

						// uni.uploadFile({
						// 	url: this.action,
						// 	filePath: res.tempFilePaths[0],
						// 	name: this.fileName,
						// 	formData: this.formData,
						// 	fileType: 'image',
						// 	header: this.header,
						// 	success: (res) => {
						// 		let result = this.uploadSuccess(res);
						// 		if (result.success) {
						// 			this.add('image', result.url)
						// 		} else {
						// 			uni.showToast({
						// 				title: '上传失败'
						// 			});
						// 		}
						// 	},
						// 	fail: (err) => {
						// 		let result = this.uploadSuccess(res);
						// 		if (result.success) {
						// 			this.add('image', result.url)
						// 		} else {
						// 			uni.showToast({
						// 				title: '上传失败'
						// 			});
						// 		}
						// 	},
						// 	complete: () => {
						// 		uni.hideLoading()
						// 	}
						// });
					}
				});
			},
			// 选择视频并上传
			chooseVideo() {
				uni.chooseVideo({
					count: 1,
					sourceType: ['camera', 'album'],
					success: (res) => {
						if (this.videoMaxSize && res.tempFile.size > this.videoMaxSize * 1024 * 1024) {
							uni.showModal({
								title: '提示',
								content: `文件过大，请上传小于${this.videoMaxSize}MB的文件`,
								showCancel: false
							})
							return;
						}
						uni.showLoading({
							title: '上传中',
							mask: true
						});

						uni.uploadFile({
							url: this.action,
							filePath: res.tempFilePath,
							name: this.fileName,
							formData: this.formData,
							fileType: 'video',
							header: this.header,
							success: (res) => {
								let result = this.uploadSuccess(res);
								if (result.success) {
									this.add('video', result.url)
								} else {
									uni.showToast({
										title: '上传失败'
									});
								}
							},
							fail: (err) => {
								let result = this.uploadSuccess(res);
								if (result.success) {
									this.add('video', result.url)
								} else {
									uni.showToast({
										title: '上传失败'
									});
								}
							},
							complete: () => {
								uni.hideLoading()
							}
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.workspace {

		// padding: 0 20rpx;
		&.onlyshow {
			padding: 0;

			.item-box {
				border: none;
				margin-bottom: 0;

				.text {
					white-space: pre-wrap;
					border-bottom: 0;
					padding: 0rpx 20rpx;
					margin-bottom: 10rpx;
				}

				.image {
					display: block;
				}
			}
		}

		.item-box {
			border: 1rpx solid #eee;
			margin-bottom: 10rpx;
			border-radius: 5rpx;

			.operation {
				height: 50rpx;
				display: flex;
				justify-content: flex-end;
			}

			.btn {

				width: 50rpx;
				height: 50rpx;

				.image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.textarea {
				padding: 20rpx;
				width: calc(100% - 40rpx);
				// box-sizing: border-box;
				border-bottom: 1rpx dashed #eee;
				margin-bottom: 15rpx;
				line-height: 30rpx;
			}
		}
	}

	.btn-box {
		height: 80rpx;
		display: flex;
		justify-content: flex-end;
		align-items: center;

		.btn {
			display: flex;
			font-size: 35rpx;
			border-radius: 50%;
			justify-content: center;
			align-items: center;
			width: 60rpx;
			height: 60rpx;
			background-color: #FFFFFF;
			color: #0081FF;

			.image {
				width: 40rpx;
				height: 40rpx;
			}

			&.btn-hover {
				background-color: #eee;
			}
		}
	}

	.tips {
		font-size: 24rpx;
		color: #b5bdce;
		text-align: center;
		padding: 24rpx;
	}
</style>
